using System;
using System.Web.Mvc;
using Netkk.Common;
using Netkk.Common.Interfaces.Services;
using Netkk.Core;
using Netkk.Common.Models;
using Netkk.Core.Auth;
using Netkk.Models;


namespace Netkk.Controllers
{
    [HandleError]
    public class ContentController : BaseControler
    {
        private const string Status404 = "404 - Seite konnte nicht gefunden werden";

        private readonly IContentService contentService;

        public ContentController(IContentService contentService)
        {
            this.contentService = contentService;
        }

        public ActionResult Show(string id)
        {
            ContentModel model = ContentModel.Create(contentService.GetContentByName(id.ToLower()));

            if (model == null)
            {
                model = NewContent(id.ToLower(), Status404);
            }

            model.ShowEditLink = this.IsInRole(Roles.Admin);

            return View(model);
        }

        private static ContentModel NewContent(string name, string value)
        {
            return new ContentModel
            {
                Name = name,
                PageTitle = value,
                PageContent = value
            };
        }

        [UserInRole(Roles.Admin)]
        public ActionResult Edit(string id)
        {
            ContentModel model = ContentModel.Create(contentService.GetContentByName(id.ToLower()));

            if (model == null)
            {
                model = NewContent(id.ToLower(), Status404);
            }

            return View(model);
        }

        [UserInRole(Roles.Admin)]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Edit(string id, FormCollection formValues)
        {
            ContentModel model = ContentModel.Create(contentService.GetContentByName(id.ToLower()));
            try
            {
                if (model == null)
                {
                    model = NewContent(id.ToLower(), String.Empty);
                }

                UpdateModel(model, new[] { "PageTitle", "PageDescription", "PageKeywords", "PageContent" });

                Content content = model.ToContent();

                if (!contentService.SaveContent(content))
                {
                    return View(model);
                }

                return this.RedirectToActionWithId("Content", "Show", id);
                
            }
            catch
            {
                return View(model);
            }
        }

        [UserInRole(Roles.Admin)]
        public ActionResult Delete(string id)
        {
            Content content = contentService.GetContentByName(id.ToLower());
            try
            {
                contentService.DeleteContent(content);

                return RedirectToAction("Index", "Home");
            }
            catch
            {
                return View(content);
            }
        }

    }
}
